package com.hxtec.polaris.service.impl;

import com.hxtec.polaris.commons.api.vo.Result;
import com.hxtec.polaris.commons.constant.Code;
import com.hxtec.polaris.commons.constant.Log;
import com.hxtec.polaris.commons.constant.Msg;
import com.hxtec.polaris.commons.dto.Page;
import com.hxtec.polaris.commons.exception.MyException;
import com.hxtec.polaris.entity.ShopCarouse;
import com.hxtec.polaris.mapper.ShopCarouseMapper;
import com.hxtec.polaris.service.CarouseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Describtion 轮播图相关service
 * @Author yonyong
 * @Date 2019/11/14 11:01
 * @Version 1.0.0
 **/
@Service
@Transactional
public class CarouseServiceImpl implements CarouseService{

    @Resource
    private ShopCarouseMapper shopCarouseMapper;

    @Override
    public Object getAll() {
        List<ShopCarouse> list = shopCarouseMapper.getAll();
        if (null != list && list.size()>0){
            return Result.ok(list);
        }else {
            throw new MyException(Result.error(Code.FAIL_4201, Msg.GET_CAROUSE_NULL));
        }
    }

    @Override
    public Object deleteCarouse(int[] ids) {
        try{
            doDeleteCarouse(ids);
            return Result.ok();
        }catch (Exception e){
            String params = "ids="+ Arrays.toString(ids);
            String LOG_MSG = MessageFormat.format(Log.PATTERN_LOG,Log.DELETE,"CarouseServiceImpl", "deleteCarouse",params);
            throw new MyException(Result.error(Code.FAIL_4201, Msg.CATEGORY_DELETE_FAIL),LOG_MSG);
        }
    }

    /**
     * 执行删除轮播图
     * @param ids
     */
    private void doDeleteCarouse(int[] ids) {
        for (int i : ids){
            shopCarouseMapper.doDeleteCarouse(i);
        }
    }

    @Override
    public Object addCarouse(ShopCarouse shopCarouse) {
        try{
            doAddCarouse(shopCarouse);
            return Result.ok();
        }catch (Exception e){
            String params = "shopCarouse="+shopCarouse.toString();
            String LOG_MSG = MessageFormat.format(Log.PATTERN_LOG,Log.DELETE,"CarouseServiceImpl", "addCarouse",params);
            throw new MyException(Result.error(Code.FAIL_4201, Msg.CATEGORY_DELETE_FAIL),LOG_MSG);
        }
    }

    /**
     * 执行添加轮播图
     * @param shopCarouse
     */
    private void doAddCarouse(ShopCarouse shopCarouse) {
        shopCarouseMapper.doAddCarouse(shopCarouse);
    }

    @Override
    public Object updateCarouse(ShopCarouse shopCarouse) {
        try{
            doUpdateCarouse(shopCarouse);
            return Result.ok();
        }catch (Exception e){
            String params = "shopCarouse="+shopCarouse.toString();
            String LOG_MSG = MessageFormat.format(Log.PATTERN_LOG,Log.UPDATE,"CarouseServiceImpl", "updateCarouse",params);
            throw new MyException(Result.error(Code.FAIL_4201, Msg.COMMON_FAIL),LOG_MSG);
        }
    }

    @Override
    public Object getList(Integer pageNow, Integer rows, String condition, String isDeleted) {
        try{
            return doGetList(pageNow,rows,condition,isDeleted);
        }catch (Exception e){
            String params = "pageNow="+pageNow +";rows="+rows +";condition="+condition +";isDeleted="+isDeleted;
            String LOG_MSG = MessageFormat.format(Log.PATTERN_LOG,Log.QUERY,"CarouseServiceImpl", "getList",params);
            throw new MyException(Result.error(Code.FAIL_4201, Msg.COMMON_FAIL),LOG_MSG);
        }
    }

    /**
     * 执行分页查询
     * @param pageNow
     * @param rows
     * @param condition
     * @param isDeleted
     * @return
     */
    private Result doGetList(Integer pageNow, Integer rows, String condition, String isDeleted) {
        //分页信息
        int count = shopCarouseMapper.doGetListCount(pageNow,rows,condition,isDeleted);
        Page page = new Page(count,pageNow,rows);

        //数据信息
        //分页查出订单信息
        List<Map<String,Object>> list = shopCarouseMapper.doGetList((pageNow-1)*rows,rows,condition,isDeleted);
        return Result.ok(page,list);
    }

    @Override
    public Object upDown(int id, String isDeleted) {
        try{
            doUpDown(id,isDeleted);
            return Result.ok();
        }catch (Exception e){
            String params = "id="+id +";isDeleted="+isDeleted;
            String LOG_MSG = MessageFormat.format(Log.PATTERN_LOG,Log.DELETE,"CarouseServiceImpl", "upDown",params);
            throw new MyException(Result.error(Code.FAIL_4201, Msg.CATEGORY_DELETE_FAIL),LOG_MSG);
        }
    }

    /**
     * 执行更新上下架
     * @param id
     * @param isDeleted
     */
    private void doUpDown(int id, String isDeleted) {
        shopCarouseMapper.doUpDown(id,isDeleted);
    }

    /**
     * 执行更新轮播图
     * @param shopCarouse
     */
    private void doUpdateCarouse(ShopCarouse shopCarouse) {
        shopCarouseMapper.doUpdateCarouse(shopCarouse);
    }
}
